package com.tanhua.server.controller;

import com.tanhua.server.service.VideoMQService;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private MovementsController movementsController;

    @Autowired
    private CommentsController commentsController;

    @Autowired
    private VideoMQService videoMQService;

    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam(value = "videoThumbnail", required = false) MultipartFile picFile,
                                          @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {
        //参数分别对应小视频和视频封面图片,视频文件上传FastDFS,封面图片上传至阿里云oss
        try {
            String videoId = this.videoService.saveVideo(picFile, videoFile);
            if (StringUtils.isNotEmpty(videoId)) {
                //发送消息: 1-发动态，2-点赞， 3-取消点赞，4-评论
                videoMQService.sendMsg(videoId, 1);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.videoService.queryVideoList(page, pageSize);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //视频点赞
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeCommentToVideo(@PathVariable("id") String videoId) {

        ResponseEntity<Long> entity = this.movementsController.likeComment(videoId);

        if (entity.getStatusCode().is2xxSuccessful()) {
            //发送消息: 1-发动态，2-点赞， 3-取消点赞，4-评论
            videoMQService.sendMsg(videoId, 2);
        }

        return entity;
    }

    //取消点赞
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> dislikeCommentToVideo(@PathVariable("id") String videoId) {
        ResponseEntity<Long> entity = this.movementsController.dislikeComment(videoId);

        if (entity.getStatusCode().is2xxSuccessful()) {
            //发送消息: 1-发动态，2-点赞， 3-取消点赞，4-评论
            videoMQService.sendMsg(videoId, 3);
        }

        return entity;
    }

    //评论列表
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentList(@PathVariable("id") String videoId,
                                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.commentsController.queryCommentList(videoId, page, pageSize);
    }

    //提交评论
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param,
                                             @PathVariable("id") String videoId) {
        param.put("movementId", videoId);

        ResponseEntity<Void> entity = this.commentsController.saveComments(param);
        if (entity.getStatusCode().is2xxSuccessful()) {
            //发送消息: 1-发动态，2-点赞， 3-取消点赞，4-评论
            videoMQService.sendMsg(videoId, 4);
        }

        return entity;
    }

    // 评论点赞
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> likeCommentToComment(@PathVariable("id") String publishId) {
        return this.movementsController.likeComment(publishId);
    }

    // 评论取消点赞
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> dislikeCommentToComment(@PathVariable("id") String publishId) {
        return this.movementsController.dislikeComment(publishId);
    }

    //视频用户关注
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void> saveUserFocusComments(@PathVariable("id") Long userId) { //要关注的用户id
        try {
            Boolean bool = this.videoService.followUser(userId);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //取消视频用户关注
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void> saveUserUnFocusComments(@PathVariable("id") Long userId) {//要关注的用户id
        try {
            Boolean bool = this.videoService.disFollowUser(userId);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
